package com.zj.base.httpclien;

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;

import java.io.Serializable;
import java.util.List;


/**
 * Created by zhaojie on 2017/10/14.
 * <p>
 * 解析当前服务器返回的对象
 * <p>
 * 使用方法  ： BaseInfo.Init().getArryInfo() ->将Json转为数组对象
 * 使用方法  ： BaseInfo.Init().getInfo() ->将Json转为单一对象
 */


@SuppressWarnings("unused")
public class BaseInfo implements Serializable {

    private static BaseInfo info;
    private static String replacement;

    private BaseInfo() {
    }

    public static BaseInfo init() {
        replacement = "";
        if (info == null) info = new BaseInfo();
        return info;
    }

    //将Json转为对象集合
    public <T> List<T> getArrayInfo(String str, Class<T> c) {

        try {
            String s = getData(str);
            return getArray(s, c);
        } catch (Exception e) {
            Log.e("BaseInfo.error:", "case json().fromJson.error " + e.toString());
        }
        return null;
    }

    //直接将字符串转为某一对象
    public <T> T getInfoByJson(String str, Class<T> c) {
        try {
            return JSON.parseObject(str, c);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    } //直接将涵盖基础类型字段的Json转为某一对象

    public <T> T getInfo(String str, Class<T> c) {
        try {
            String data = getData(str);
            return JSON.parseObject(data, c);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private <T> List<T> getArray(String str, Class<T> c) {
        return JSON.parseArray(str, c);
    }

    //单独获取json中配置为 data的字符串
    public String getData(String str) {
        try {
            JsonInfo j = JSON.parseObject(str, JsonInfo.class);
            if (!TextUtils.isEmpty(replacement)) {
                JSONObject jsonObject = JSON.parseObject(j.data);
                return jsonObject.getString(replacement);
            } else
                return j.data;
        } catch (Exception e) {
            e.printStackTrace();
            return "null";
        }
    }

    //在获取结果时需要忽略的字段，如果存在,需在调用init之后，其他方法之前调用;
    public BaseInfo replacement(String replace) {
        replacement = replace;
        return this;
    }


    public JsonInfo getResultInfo(String str) {
        return JSON.parseObject(str, JsonInfo.class);
    }


    public static class JsonInfo implements Serializable {
        //服务器返回码 200
        @JSONField(name = "returnStatus")
        public int error_code;

        //服务器操作信息
        @JSONField(name = "returnReason")
        public String error_message;

        //整体json
        @JSONField(name = "returnInformation")
        public String data;

        public boolean isSucess() {
            return error_code == 200;
        }

        public String showMsg() {
            return "ServerResponse  -----   error_code  =  " + error_code + "      erroMsg   =  " + error_message;
        }
    }
}
